package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.home.entity.UserInfo;
import com.rongshu.view.userInfo.dialog.ChiefExperienceOfficerDialog;
import com.rongshu.view.userInfo.entity.AccountInformation;
import com.rongshu.view.userInfo.iview.PersonDetailsView;
import com.rongshu.view.userInfo.presenter.PersonDetailsPresenter;
import com.rongshu.view.web.LocalWebActivity;
import com.rongshu.widget.BanyanRatingBar;
import com.rongshu.widget.PraiseNumDialog;
import com.utils.DensityUtil;
import com.widget.CircleImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class PersonDetailsActivity extends BaseActivity<PersonDetailsPresenter> implements PersonDetailsView {
    @BindView(R.id.iv_bg_icon)
    ImageView iv_bg_icon;
    @BindView(R.id.iv_head_icon)
    CircleImageView iv_head_icon;
    @BindView(R.id.iv_vip)
    ImageView iv_vip;
    @BindView(R.id.txt_article_author)
    TextView txt_article_author;
    @BindView(R.id.iv_create_level)
    ImageView iv_create_level;
    @BindView(R.id.txt_write_value)
    TextView txt_write_value;
    @BindView(R.id.txt_write_end)
    TextView txt_write_end;
    @BindView(R.id.write_progressbar)
    ProgressBar write_progressbar;
    @BindView(R.id.iv_read_level)
    ImageView iv_read_level;
    @BindView(R.id.txt_read_value)
    TextView txt_read_value;
    @BindView(R.id.txt_read_end)
    TextView txt_read_end;
    @BindView(R.id.read_progressbar)
    ProgressBar read_progressbar;
    @BindView(R.id.banyan_ratingbar_write)
    BanyanRatingBar banyan_ratingbar_write;
    @BindView(R.id.banyan_ratingbar_reader)
    BanyanRatingBar banyan_ratingbar_reader;
    @BindView(R.id.txt_signature)
    TextView txt_signature;
    @BindView(R.id.layout_sex)
    LinearLayout layout_sex;
    @BindView(R.id.user_sex_iv)
    ImageView user_sex_iv;
    @BindView(R.id.user_sex_tv)
    TextView user_sex_tv;
    @BindView(R.id.user_city)
    TextView user_city;
    @BindView(R.id.get_praise_num)
    TextView get_praise_num;
    @BindView(R.id.keep_num)
    TextView keep_num;
    @BindView(R.id.fans_num)
    TextView fans_num;
    @BindView(R.id.penpal_num)
    TextView penpal_num;
    @BindView(R.id.layout_detail)
    RelativeLayout layout_detail;
    @BindView(R.id.iv_chief)
    ImageView ivChief;


    //Intent
    private Intent intent = null;
    //用户信息
    private UserInfo.ResponseBean userInfo = null;
    //账号信息
    private AccountInformation.ResponseBean information = null;
    //map
    private Map<String, Object> map = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hintTitleBar();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_person_details_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new PersonDetailsPresenter(this), this);
        //设置星星大小
        banyan_ratingbar_write.setSize(DensityUtil.dip2px(this, 16), DensityUtil.dip2px(this, 16));
        banyan_ratingbar_reader.setSize(DensityUtil.dip2px(this, 16), DensityUtil.dip2px(this, 16));
        RelativeLayout.LayoutParams layoutParams3 = (RelativeLayout.LayoutParams) iv_bg_icon.getLayoutParams();
        int screenWidth = DensityUtil.getScreenWidth(this);
        final int bpgHight = screenWidth / 4 * 3;
        layoutParams3.height = bpgHight;
        iv_bg_icon.setLayoutParams(layoutParams3);
        final RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) layout_detail.getLayoutParams();
        layout_detail.post(new Runnable() {
            @Override
            public void run() {
                int detailHight = layout_detail.getHeight();
                layoutParams2.topMargin = bpgHight - detailHight / 2;
                layout_detail.setLayoutParams(layoutParams2);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserInfo();
        getAccountModel();
    }

    /**
     * 获取用户资料
     */
    public void getUserInfo() {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this);
            return;
        }
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.getUserInfo(user_id, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 账户信息
     */
    public void getAccountModel() {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this);
            return;
        }
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.getAccountModel(user_id, Common.encryptionSign(map));
        map.clear();
    }


    /**
     * 用户信息
     */
    private void updateUserInformation(UserInfo.ResponseBean userInfo) {
        if (userInfo == null) {
            return;
        }
        this.userInfo = userInfo;
        iv_head_icon.setBackground(ContextCompat.getDrawable(PersonDetailsActivity.this, R.drawable.shape_stroke_white_background_5dp));
        //头像
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.user_head_icon);
        Glide.with(this)
                .load(userInfo.getAvatar())
                .apply(options)
                .into(iv_head_icon);
        //背景
        options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.user_top_bg);
        Glide.with(this)
                .load(userInfo.getBgp())
                .apply(options)
                .into(iv_bg_icon);
        //vip
        iv_vip.setVisibility(userInfo.isVip() ? View.VISIBLE : View.GONE);
        //name
        txt_article_author.setText(userInfo.getBi_name());
        //简介
        txt_signature.setText(TextUtils.isEmpty(userInfo.getSignature()) ? "暂无简介" : userInfo.getSignature());
        //性别
        String sex = userInfo.getSex();
        layout_sex.setVisibility(TextUtils.isEmpty(sex) ? View.GONE : View.VISIBLE);
        user_sex_iv.setBackgroundResource(TextUtils.isEmpty(sex) ? R.drawable.man_icon :
                sex.equals("男") ? R.drawable.man_icon : R.drawable.women_icon);
        user_sex_tv.setText(TextUtils.isEmpty(sex) ? "无" : sex);
        //地址
        user_city.setText(TextUtils.isEmpty(userInfo.getCity()) ? "暂无" : userInfo.getCity());
        //首席
        String reg_time = userInfo.getReg_time();
        long registration_date = DateUtils.parseDate(DateUtils.YYYY_MM_DD, reg_time).getTime();
        String time = "2019-02-25";
        long reduced_date = DateUtils.parseDate(DateUtils.YYYY_MM_DD, time).getTime();
        if (registration_date <= reduced_date) {
            ivChief.setVisibility(View.VISIBLE);
        } else {
            ivChief.setVisibility(View.GONE);
        }
    }

    /**
     * 账号信息
     */
    private void updateAccountInformation(AccountInformation.ResponseBean information) {
        if (information == null) {
            return;
        }
        this.information = information;
        //获赞，关注，粉丝，笔友会
        get_praise_num.setText(String.valueOf(information.getArticleLikeCount()));
        keep_num.setText(String.valueOf(information.getGuanzhu()));
        fans_num.setText(String.valueOf(information.getFensi()));
        penpal_num.setText(String.valueOf(information.getPenPalwillCount()));
        //创作等级
        setCreateLevel(information.getCreate_name(), iv_create_level);
        //创作星星
        banyan_ratingbar_write.setProgress(information.getCreate_xing_num());
        //创作已有经验
        txt_write_value.setText(information.getCreate_value() + "/");
        //创作结束经验
        txt_write_end.setText(information.getCreate_fenshu() + "");
        //创作进度条
        write_progressbar.setMax(information.getCreate_fenshu());
        write_progressbar.setProgress(information.getCreate_value());
        //阅读等级
        setReaderLevel(information.getRead_name(), iv_read_level);
        //阅读星星
        banyan_ratingbar_reader.setProgress(information.getRead_xing_num());
        //阅读已有经验
        txt_read_value.setText(information.getRead_value() + "/");
        //阅读结束经验
        txt_read_end.setText(information.getRead_fenshu() + "");
        //阅读进度条
        read_progressbar.setMax(information.getRead_fenshu());
        read_progressbar.setProgress(information.getRead_value());
    }

    /**
     * 创作等级
     */
    public void setCreateLevel(String level, ImageView view) {
        if (TextUtils.isEmpty(level)) {
            return;
        }
        if (level.equals("1")) {
            view.setImageResource(R.drawable.level_green_1);
        } else if (level.equals("2")) {
            view.setImageResource(R.drawable.level_green_2);
        } else if (level.equals("3")) {
            view.setImageResource(R.drawable.level_green_3);
        } else if (level.equals("4")) {
            view.setImageResource(R.drawable.level_green_4);
        } else if (level.equals("5")) {
            view.setImageResource(R.drawable.level_green_5);
        }
    }

    /**
     * 阅读等级
     */
    public void setReaderLevel(String level, ImageView view) {
        if (TextUtils.isEmpty(level)) {
            return;
        }
        if (level.equals("1")) {
            view.setImageResource(R.drawable.level_read_green_1);
        } else if (level.equals("2")) {
            view.setImageResource(R.drawable.level_read_green_2);
        } else if (level.equals("3")) {
            view.setImageResource(R.drawable.level_read_green_3);
        } else if (level.equals("4")) {
            view.setImageResource(R.drawable.level_read_green_4);
        } else if (level.equals("5")) {
            view.setImageResource(R.drawable.level_read_green_5);
        }
    }

    @OnClick({R.id.head_left, R.id.head_edit, R.id.write_tips, R.id.read_tips, R.id.iv_chief})
    public void onClick(View view) {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        ArrayList<String> list = null;
        switch (view.getId()) {
            case R.id.head_left:
                finish();
                break;
            case R.id.head_edit://编辑
                intent = new Intent(this, PerfectInfomationActivity.class);
                if (userInfo != null) {
                    intent.putExtra(Constant.INTENT_DATA, userInfo.getUser_id());
                }
                startActivity(intent);
                break;
            case R.id.get_praise_layout://获赞
                if (userInfo == null) {
                    return;
                }
                PraiseNumDialog.Builder builder = new PraiseNumDialog.Builder(this);
                builder.setPrarseNum(information.getArticleLikeCount());
                PraiseNumDialog praiseNumDialog = builder.create();
                praiseNumDialog.show();
                break;
            case R.id.keep_layout://关注
                if (user_id == 0) {
                    LoginUtils.toLogin(this);
                    return;
                }
                intent = new Intent(PersonDetailsActivity.this, AttentionAcitivity.class);
                startActivity(intent);
                break;
            case R.id.fans_layout://粉丝
                if (user_id == 0) {
                    LoginUtils.toLogin(this);
                    return;
                }
                intent = new Intent(PersonDetailsActivity.this, FansActivity.class);
                startActivity(intent);
                break;
            case R.id.penpal_Layout://笔友会
                if (user_id == 0) {
                    LoginUtils.toLogin(this);
                    return;
                }
                if (userInfo == null) {
                    return;
                }
                intent = new Intent(PersonDetailsActivity.this, PenpalWillActivity.class);
                if (userInfo != null) {
                    intent.putExtra(Constant.USER_ID, userInfo.getUser_id());
                }
                startActivity(intent);
                break;
            case R.id.write_tips://写作说明
                intent = new Intent(this, LocalWebActivity.class);
                intent.putExtra(Constant.INTENT_TITLE, "创作说明");
                intent.putExtra(Constant.INTENT_H5, Common.CREATE_EXP);
                startActivity(intent);
                break;
            case R.id.read_tips://阅读说明
                intent = new Intent(this, LocalWebActivity.class);
                intent.putExtra(Constant.INTENT_TITLE, "阅读说明");
                intent.putExtra(Constant.INTENT_H5, Common.READER_EXP);
                startActivity(intent);
                break;
            case R.id.iv_chief:
                ChiefExperienceOfficerDialog.Builder builder1 = new ChiefExperienceOfficerDialog.Builder(this);
                ChiefExperienceOfficerDialog dialog = builder1.create();
                dialog.show();
                break;
        }
    }

    /**
     * 用户信息
     *
     * @param userInfo
     */
    @Override
    public void onUserInfo(UserInfo userInfo) {
        if (userInfo.getErrCode() == 0) {
            UserInfo.ResponseBean responseBean = userInfo.getResponse();
            updateUserInformation(responseBean);
        }
    }

    /**
     * 账号信息
     *
     * @param information
     */
    @Override
    public void onAccountInfo(AccountInformation information) {
        if (information.getErrCode() == 0) {
            AccountInformation.ResponseBean responseBean = information.getResponse();
            updateAccountInformation(responseBean);
        }
    }
}
